<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java 基本数据类型</h1>
        <p>变量就是申请内存来存储值。也就是说，当创建变量的时候，需要在内存中申请空间。</p>
        <p>内存管理系统根据变量的类型为变量分配存储空间，分配的空间只能用来储存该类型数据。</p>
        <p>
            <img src="https://www.runoob.com/wp-content/uploads/2013/12/memorypic1.jpg"></p>
        <p>因此，通过定义不同类型的变量，可以在内存中储存整数、小数或者字符。</p>
        <p>Java 的两大数据类型:</p>
        <ul><li>内置数据类型</li>
            <li>引用数据类型</li>
        </ul><hr>
        <h2>内置数据类型</h2>
        <p>Java语言提供了八种基本类型。六种数字类型（四个整数型，两个浮点型），一种字符类型，还有一种布尔型。 </p>
        <p><strong>byte：</strong></p>
        <ul>
            <li>
                byte 数据类型是8位、有符号的，以二进制补码表示的整数；</li>
            <li>
                最小值是 <span class="marked">-128（-2^7）</span>；</li>
            <li>
                最大值是 <span class="marked">127（2^7-1）</span>；</li>
            <li>
                默认值是 <span class="marked">0</span>；</li>
            <li>
                byte 类型用在大型数组中节约空间，主要代替整数，因为 byte 变量占用的空间只有 int 类型的四分之一；</li>
            <li>
                例子：byte a = 100，byte b = -50。</li>
        </ul>
        <p><strong>short：</strong></p>
        <ul>
            <li>
                short 数据类型是 16 位、有符号的以二进制补码表示的整数</li>
            <li>
                最小值是 <span class="marked">-32768（-2^15）</span>；</li>
            <li>
                最大值是 <span class="marked">32767（2^15 - 1）</span>；</li>
            <li>
                Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一；</li>
            <li>
                默认值是 <span class="marked">0</span>；</li>
            <li>
                例子：short s = 1000，short r = -20000。</li>
        </ul>
        <p><strong>int：</strong></p>
        <ul>
            <li>
                int 数据类型是32位、有符号的以二进制补码表示的整数；</li>
            <li>
                最小值是 <span class="marked">-2,147,483,648（-2^31）</span>；</li>
            <li>
                最大值是 <span class="marked">2,147,483,647（2^31 - 1）</span>；</li>
            <li>
                一般地整型变量默认为 int 类型；</li>
            <li>
                默认值是 <span class="marked">0</span> ；</li>
            <li>
                例子：int a = 100000, int b = -200000。</li>
        </ul>
        <p><strong>long：</strong></p>
        <ul>
            <li>
                long 数据类型是 64 位、有符号的以二进制补码表示的整数；</li>
            <li>
                最小值是 <span class="marked">-9,223,372,036,854,775,808（-2^63）</span>；</li>
            <li>
                最大值是 <span class="marked">9,223,372,036,854,775,807（2^63 -1）</span>；</li>
            <li>
                这种类型主要使用在需要比较大整数的系统上；</li>
            <li>
                默认值是 <span class="marked">0L</span>；</li>
            <li>
                例子： long a = 100000L，Long b = -200000L。<br>
                "L"理论上不分大小写，但是若写成"l"容易与数字"1"混淆，不容易分辩。所以最好大写。
            </li>
        </ul>
        <p><strong>float：</strong></p>
        <ul>
            <li>
                float 数据类型是单精度、32位、符合IEEE 754标准的浮点数；</li>
            <li>
                float 在储存大型浮点数组的时候可节省内存空间；</li>
            <li>
                默认值是 <span class="marked">0.0f</span>；</li>
            <li>
                浮点数不能用来表示精确的值，如货币；</li>
            <li>
                例子：float f1 = 234.5f。</li>
        </ul>
        <p><strong>double：</strong></p>
        <ul>
            <li>
                double 数据类型是双精度、64 位、符合IEEE 754标准的浮点数；</li>
            <li>
                浮点数的默认类型为double类型；</li>
            <li>
                double类型同样不能表示精确的值，如货币；</li>
            <li>
                默认值是 <span class="marked">0.0d</span>；</li>
            <li>
                例子：double d1 = 123.4。</li>
        </ul>
        <p><strong>boolean：</strong></p>
        <ul>
            <li>
                boolean数据类型表示一位的信息；</li>
            <li>
                只有两个取值：true 和 false；</li>
            <li>
                这种类型只作为一种标志来记录 true/false 情况；</li>
            <li>
                默认值是 <span class="marked">false</span>；</li>
            <li>
                例子：boolean one = true。</li>
        </ul>
        <p><strong>char：</strong></p>
        <ul>
            <li>
                char类型是一个单一的 16 位 Unicode 字符；</li>
            <li>
                最小值是 <span class="marked">\u0000</span>（即为0）；</li>
            <li>
                最大值是 <span class="marked">\uffff</span>（即为65,535）；</li>
            <li>
                char 数据类型可以储存任何字符；</li>
            <li>
                例子：char letter = 'A';。</li>
        </ul>
        <h3>实例</h3>
        <p>对于数值类型的基本类型的取值范围，我们无需强制去记忆，因为它们的值都已经以常量的形式定义在对应的包装类中了。请看下面的例子：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">PrimitiveTypeTest</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> byte  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">基本类型：byte 二进制位数：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Byte</span><span class="hl-code">.</span><span class="hl-identifier">SIZE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">包装类：java.lang.Byte</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值：Byte.MIN_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Byte</span><span class="hl-code">.</span><span class="hl-identifier">MIN_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值：Byte.MAX_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Byte</span><span class="hl-code">.</span><span class="hl-identifier">MAX_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-comment">//</span><span class="hl-comment"> short  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">基本类型：short 二进制位数：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Short</span><span class="hl-code">.</span><span class="hl-identifier">SIZE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">包装类：java.lang.Short</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值：Short.MIN_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Short</span><span class="hl-code">.</span><span class="hl-identifier">MIN_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值：Short.MAX_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Short</span><span class="hl-code">.</span><span class="hl-identifier">MAX_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-comment">//</span><span class="hl-comment"> int  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">基本类型：int 二进制位数：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Integer</span><span class="hl-code">.</span><span class="hl-identifier">SIZE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">包装类：java.lang.Integer</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值：Integer.MIN_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Integer</span><span class="hl-code">.</span><span class="hl-identifier">MIN_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值：Integer.MAX_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Integer</span><span class="hl-code">.</span><span class="hl-identifier">MAX_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-comment">//</span><span class="hl-comment"> long  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">基本类型：long 二进制位数：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Long</span><span class="hl-code">.</span><span class="hl-identifier">SIZE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">包装类：java.lang.Long</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值：Long.MIN_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Long</span><span class="hl-code">.</span><span class="hl-identifier">MIN_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值：Long.MAX_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Long</span><span class="hl-code">.</span><span class="hl-identifier">MAX_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-comment">//</span><span class="hl-comment"> float  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">基本类型：float 二进制位数：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Float</span><span class="hl-code">.</span><span class="hl-identifier">SIZE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">包装类：java.lang.Float</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值：Float.MIN_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Float</span><span class="hl-code">.</span><span class="hl-identifier">MIN_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值：Float.MAX_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Float</span><span class="hl-code">.</span><span class="hl-identifier">MAX_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-comment">//</span><span class="hl-comment"> double  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">基本类型：double 二进制位数：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Double</span><span class="hl-code">.</span><span class="hl-identifier">SIZE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">包装类：java.lang.Double</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值：Double.MIN_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Double</span><span class="hl-code">.</span><span class="hl-identifier">MIN_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值：Double.MAX_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Double</span><span class="hl-code">.</span><span class="hl-identifier">MAX_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-comment">//</span><span class="hl-comment"> char  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">基本类型：char 二进制位数：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">Character</span><span class="hl-code">.</span><span class="hl-identifier">SIZE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">包装类：java.lang.Character</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 以数值形式而不是字符形式将Character.MIN_VALUE输出到控制台  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值：Character.MIN_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code">
                + </span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">Character</span><span class="hl-code">.</span><span class="hl-identifier">MIN_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 以数值形式而不是字符形式将Character.MAX_VALUE输出到控制台  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值：Character.MAX_VALUE=</span><span class="hl-quotes">"</span><span class="hl-code">
                + </span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">Character</span><span class="hl-code">.</span><span class="hl-identifier">MAX_VALUE</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
            </div>
            <br>

        </div>
        <p>编译以上代码输出结果如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">基本类型：</span><span class="kwd">byte</span><span class="pln"> </span><span class="pun">二进制位数：</span><span class="lit">8</span><span class="pln">
</span><span class="pun">包装类：</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Byte</span><span class="pln">
</span><span class="pun">最小值：</span><span class="typ">Byte</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">=-</span><span class="lit">128</span><span class="pln">
</span><span class="pun">最大值：</span><span class="typ">Byte</span><span class="pun">.</span><span class="pln">MAX_VALUE</span><span class="pun">=</span><span class="lit">127</span><span class="pln">

</span><span class="pun">基本类型：</span><span class="kwd">short</span><span class="pln"> </span><span class="pun">二进制位数：</span><span class="lit">16</span><span class="pln">
</span><span class="pun">包装类：</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Short</span><span class="pln">
</span><span class="pun">最小值：</span><span class="typ">Short</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">=-</span><span class="lit">32768</span><span class="pln">
</span><span class="pun">最大值：</span><span class="typ">Short</span><span class="pun">.</span><span class="pln">MAX_VALUE</span><span class="pun">=</span><span class="lit">32767</span><span class="pln">

</span><span class="pun">基本类型：</span><span class="kwd">int</span><span class="pln"> </span><span class="pun">二进制位数：</span><span class="lit">32</span><span class="pln">
</span><span class="pun">包装类：</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Integer</span><span class="pln">
</span><span class="pun">最小值：</span><span class="typ">Integer</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">=-</span><span class="lit">2147483648</span><span class="pln">
</span><span class="pun">最大值：</span><span class="typ">Integer</span><span class="pun">.</span><span class="pln">MAX_VALUE</span><span class="pun">=</span><span class="lit">2147483647</span><span class="pln">

</span><span class="pun">基本类型：</span><span class="kwd">long</span><span class="pln"> </span><span class="pun">二进制位数：</span><span class="lit">64</span><span class="pln">
</span><span class="pun">包装类：</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Long</span><span class="pln">
</span><span class="pun">最小值：</span><span class="typ">Long</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">=-</span><span class="lit">9223372036854775808</span><span class="pln">
</span><span class="pun">最大值：</span><span class="typ">Long</span><span class="pun">.</span><span class="pln">MAX_VALUE</span><span class="pun">=</span><span class="lit">9223372036854775807</span><span class="pln">

</span><span class="pun">基本类型：</span><span class="kwd">float</span><span class="pln"> </span><span class="pun">二进制位数：</span><span class="lit">32</span><span class="pln">
</span><span class="pun">包装类：</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Float</span><span class="pln">
</span><span class="pun">最小值：</span><span class="typ">Float</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">=</span><span class="lit">1.4E-45</span><span class="pln">
</span><span class="pun">最大值：</span><span class="typ">Float</span><span class="pun">.</span><span class="pln">MAX_VALUE</span><span class="pun">=</span><span class="lit">3.4028235E38</span><span class="pln">

</span><span class="pun">基本类型：</span><span class="kwd">double</span><span class="pln"> </span><span class="pun">二进制位数：</span><span class="lit">64</span><span class="pln">
</span><span class="pun">包装类：</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Double</span><span class="pln">
</span><span class="pun">最小值：</span><span class="typ">Double</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">=</span><span class="lit">4.9E-324</span><span class="pln">
</span><span class="pun">最大值：</span><span class="typ">Double</span><span class="pun">.</span><span class="pln">MAX_VALUE</span><span class="pun">=</span><span class="lit">1.7976931348623157E308</span><span class="pln">

</span><span class="pun">基本类型：</span><span class="kwd">char</span><span class="pln"> </span><span class="pun">二进制位数：</span><span class="lit">16</span><span class="pln">
</span><span class="pun">包装类：</span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Character</span><span class="pln">
</span><span class="pun">最小值：</span><span class="typ">Character</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">=</span><span class="lit">0</span><span class="pln">
</span><span class="pun">最大值：</span><span class="typ">Character</span><span class="pun">.</span><span class="pln">MAX_VALUE</span><span class="pun">=</span><span class="lit">65535</span></pre>
        <p>Float和Double的最小值和最大值都是以科学记数法的形式输出的，结尾的"E+数字"表示E之前的数字要乘以10的多少次方。比如3.14E3就是3.14 × 10<sup>3</sup> =3140，3.14E-3 就是 3.14 x 10<sup>-3</sup> =0.00314。</p>
        <p>实际上，JAVA中还存在另外一种基本类型void，它也有对应的包装类 java.lang.Void，不过我们无法直接对它们进行操作。</p>
        <hr>
        <h2>
            引用类型</h2>
        <ul>
            <li>
                在Java中，引用类型的变量非常类似于C/C++的指针。引用类型指向一个对象，指向对象的变量是引用变量。这些变量在声明时被指定为一个特定的类型，比如 Employee、Puppy 等。变量一旦声明后，类型就不能被改变了。</li>
            <li>
                对象、数组都是引用数据类型。</li>
            <li>
                所有引用类型的默认值都是null。</li>
            <li>
                一个引用变量可以用来引用任何与之兼容的类型。</li>
            <li>
                例子：Site site = new Site("Runoob")。</li>
        </ul>
        <hr>
        <h2>Java 常量</h2>
        <p>常量在程序运行时是不能被修改的。</p>
        <p>在 Java 中使用 final 关键字来修饰常量，声明方式和变量类似：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">final</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> PI </span><span class="pun">=</span><span class="pln"> </span><span class="lit">3.1415927</span><span class="pun">;</span></pre>
        <p>
            虽然常量名也可以用小写，但为了便于识别，通常使用大写字母表示常量。
        </p>

        <p>字面量可以赋给任何内置类型的变量。例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">byte</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">68</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">char</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="str">'A'</span></pre>
        <p>byte、int、long、和short都可以用十进制、16进制以及8进制的方式来表示。</p>
        <p>当使用常量的时候，前缀 <span class="marked">0</span> 表示 8 进制，而前缀 <span class="marked">0x</span> 代表 16 进制, 例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">int</span><span class="pln"> </span><span class="kwd">decimal</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">100</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> octal </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0144</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> hexa </span><span class="pun">=</span><span class="pln">  </span><span class="lit">0x64</span><span class="pun">;</span></pre>
        <p>和其他语言一样，Java的字符串常量也是包含在两个引号之间的字符序列。下面是字符串型字面量的例子：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="str">"Hello World"</span><span class="pln">
</span><span class="str">"two\nlines"</span><span class="pln">
</span><span class="str">"\"This is in quotes\""</span></pre>
        <p>字符串常量和字符常量都可以包含任何Unicode字符。例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">char</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="str">'\u0001'</span><span class="pun">;</span><span class="pln">
</span><span class="typ">String</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="str">"\u0001"</span><span class="pun">;</span></pre>
        <p>
            Java语言支持一些特殊的转义字符序列。</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>
                    符号</th>
                <th>
                    字符含义</th>
            </tr>
            <tr>
                <td>
                    \n</td>
                <td>
                    换行 (0x0a)</td>
            </tr>
            <tr>
                <td>
                    \r</td>
                <td>
                    回车 (0x0d)</td>
            </tr>
            <tr>
                <td>
                    \f</td>
                <td>
                    换页符(0x0c)</td>
            </tr>
            <tr>
                <td>
                    \b</td>
                <td>
                    退格 (0x08)</td>
            </tr>
            <tr>
                <td>
                    \0</td>
                <td>
                    空字符 (0x20)</td>
            </tr>
            <tr>
                <td>
                    \s</td>
                <td>
                    字符串</td>
            </tr>
            <tr>
                <td>
                    \t</td>
                <td>
                    制表符</td>
            </tr>
            <tr>
                <td>
                    \"</td>
                <td>
                    双引号</td>
            </tr>
            <tr>
                <td>
                    \'</td>
                <td>
                    单引号</td>
            </tr>
            <tr>
                <td>
                    \\</td>
                <td>
                    反斜杠</td>
            </tr>
            <tr>
                <td>
                    \ddd</td>
                <td>
                    八进制字符 (ddd)</td>
            </tr>
            <tr>
                <td>
                    \uxxxx</td>
                <td>
                    16进制Unicode字符 (xxxx)</td>
            </tr>
            </tbody>
        </table>
        <hr>
        <h2>自动类型转换</h2>
        <p><b>整型、实型（常量）、字符型数据可以混合运算。运算中，不同类型的数据先转化为同一类型，然后进行运算。</b></p>
        <p>转换从低级到高级。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">低</span><span class="pln">  </span><span class="pun">------------------------------------&gt;</span><span class="pln">  </span><span class="pun">高</span><span class="pln">

</span><span class="kwd">byte</span><span class="pun">,</span><span class="kwd">short</span><span class="pun">,</span><span class="kwd">char</span><span class="pun">—&gt;</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> </span><span class="pun">—&gt;</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">—&gt;</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> </span><span class="pun">—&gt;</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> </span></pre>
        <p>数据类型转换必须满足如下规则：</p>
        <ul>
            <li><p>1. 不能对boolean类型进行类型转换。</p> </li><li>
            <p>2. 不能把对象类型转换成不相关类的对象。</p>  </li><li>
            <p>3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。</p>   </li><li>
            <p>4. 转换过程中可能导致溢出或损失精度，例如：</p>
            <pre class="prettyprint prettyprinted" style=""><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="lit">128</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">byte</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">byte</span><span class="pun">)</span><span class="pln">i</span><span class="pun">;</span></pre>
            <p>因为 byte 类型是 8 位，最大值为127，所以当强制转换为 int 类型值 128 时候就会导致溢出。</p>
        </li>
            <li>
                <p>5. 浮点数到整数的转换是通过舍弃小数得到，而不是四舍五入，例如：  </p>
                <pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="kwd">int</span><span class="pun">)</span><span class="lit">23.7</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="lit">23</span><span class="pun">;</span><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">)-</span><span class="lit">45.89f</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="pun">-</span><span class="lit">45</span></pre>
            </li>
        </ul>
        <h3>自动类型转换</h3>
        <p>必须满足转换前的数据类型的位数要低于转换后的数据类型，例如: short数据类型的位数为16位，就可以自动转换位数为32的int类型，同样float数据类型的位数为32，可以自动转换为64位的double类型。
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ZiDongLeiZhuan</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-types">char</span><span class="hl-code"> </span><span class="hl-identifier">c1</span><span class="hl-code">=</span><span class="hl-quotes">'</span><span class="hl-string">a</span><span class="hl-quotes">'</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">定义一个char类型</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i1</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">char自动类型转换为int</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">char自动类型转换为int后的值等于</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">i1</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-types">char</span><span class="hl-code"> </span><span class="hl-identifier">c2</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">A</span><span class="hl-quotes">'</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">定义一个char类型</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i2</span><span class="hl-code"> = </span><span class="hl-identifier">c2</span><span class="hl-code">+</span><span class="hl-number">1</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">char 类型和 int 类型计算</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">char类型和int计算后的值等于</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">i2</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
            </div>
        </div>
        <p>运行结果为:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">char</span><span class="pun">自动类型转换为</span><span class="kwd">int</span><span class="pun">后的值等于</span><span class="lit">97</span><span class="pln">
</span><span class="kwd">char</span><span class="pun">类型和</span><span class="kwd">int</span><span class="pun">计算后的值等于</span><span class="lit">66</span></pre>
        <p><strong>解析：</strong>c1的值为字符'a',查ascii码表可知对应的int类型值为97，'A'对应值为65，所以i2=65+1=66。</p>
        <h3>强制类型转换</h3>

        <ul><li><p>
            1. 条件是转换的数据类型必须是兼容的。</p></li><li><p>
            2. 格式：(type)value  type是要强制类型转换后的数据类型
            实例： </p>
            <div class="example">
                <div class="example">实例</div>
                <div class="example_code">
                    <div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">QiangZhiZhuanHuan</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i1</span><span class="hl-code"> = </span><span class="hl-number">123</span><span class="hl-code">;
        </span><span class="hl-types">byte</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-types">byte</span><span class="hl-brackets">)</span><span class="hl-identifier">i1</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">强制类型转换为byte</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">int强制类型转换为byte后的值等于</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
                </div>
            </div>
            <p>运行结果：</p>
            <pre class="prettyprint prettyprinted" style=""><span class="kwd">int</span><span class="pun">强制类型转换为</span><span class="kwd">byte</span><span class="pun">后的值等于</span><span class="lit">123</span></pre>
        </li></ul>
        <h3>隐含强制类型转换</h3>
        <ul>
            <li><p>
                1. 整数的默认类型是 int。</p></li><li><p>
            2. 浮点型不存在这种情况，因为在定义 float 类型时必须在数字后面跟上 F 或者 f。
        </p></li>
        </ul>
        <p>
            这一节讲解了 Java 的基本数据类型。下一节将探讨不同的变量类型以及它们的用法。</p>

    </div>
</div>
</body>
</html>
